<?php

namespace App\Tools;

use SimpleSoftwareIO\QrCode\BaconQrCodeGenerator;

class Qrcode
{
    protected $qrcode = null;
    public  function __construct()
    {
       $this->qrcode =  new BaconQrCodeGenerator();
    }

    /**
     * 创建二维码
     * @param $data 生产数据
     * @param $sign 二维码标识
     * @param int $size 图像大小
     * @return string
     */
    public function generate($data, $sign, $save_path, $size = 100, $level = 'H')
    {
        $path = 'storage/qrcode/'.$save_path.'/';
        if (!is_dir($path)) mkdir($path, 0755, true);
        $name = md5(date('Y-m-d H;i:s').$sign).'.png';
        $qrcode_path = $path.$name;
        $this->qrcode->margin(20)->format('png')->size($size)->errorCorrection($level)->generate($data, public_path($qrcode_path));
        return $qrcode_path;
    }
}
